package com.itboye.mengwu.app.ui.mymeng;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.BabySexPopupWindow;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.SexPopupWindow;
import com.itboye.core.utils.YesNoPopupWindow;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.BabyBean;
import com.itboye.mengwu.data.presenter.BabyPresenter;

public class ActivityBabyAdd extends BaseActivity implements Observer {

	TextView baobaoxiaoming_txt;
	BabyPresenter babyPresenter;
	TextView babybirth_txt;
	TextView babymoren_txt;
	TextView babysex_txt;
	BabyBean baby;
	long birth;
	TextView add;
	ImageView back;
	TextView txt_title;
	Calendar dateAndTime;
	BabySexPopupWindow mSexPopupWindow;
	YesNoPopupWindow yesNoPopupWindow;
	RelativeLayout baby_sex_layout, baby_birth_layout, baby_moren_layout;
	DateFormat sdf = new java.text.SimpleDateFormat("yyyy年MM月dd日");

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_baby_add;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("添加宝宝");
		// babyPresenter = new BabyPresenter(this);
		baby_birth_layout.setOnClickListener(onclicklister);
		dateAndTime = Calendar.getInstance(Locale.CHINA);
		baby_sex_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mSexPopupWindow = new BabySexPopupWindow(ActivityBabyAdd.this,
						onclicklister);
				mSexPopupWindow.showAtLocation(v, Gravity.BOTTOM
						| Gravity.CENTER_HORIZONTAL, 0, 0);
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		baby_sex_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(ActivityBabyAdd.this
								.getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
				mSexPopupWindow = new BabySexPopupWindow(ActivityBabyAdd.this,
						onclicklister);
				mSexPopupWindow.showAtLocation(v, Gravity.BOTTOM
						| Gravity.CENTER_HORIZONTAL, 0, 0);
			}
		});
		baby_moren_layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// // TODO Auto-generated method stub
				((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
						.hideSoftInputFromWindow(ActivityBabyAdd.this
								.getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
				yesNoPopupWindow = new YesNoPopupWindow(ActivityBabyAdd.this,

				onclicklister);
				yesNoPopupWindow.showAtLocation(v, Gravity.BOTTOM
						| Gravity.CENTER_HORIZONTAL, 0, 0);
			}
		});
		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				BabyPresenter babyPresenter = new BabyPresenter(
						ActivityBabyAdd.this);
				BabyBean bean = new BabyBean();
				bean.setUid((String) SPUtils.get(ActivityBabyAdd.this, null,
						SpPublicParams.UID, ""));
				if (baobaoxiaoming_txt.getText().length() > 15) {
					ByAlert.alert("宝宝名字太长了哦");
					return;
				}
				if ((baobaoxiaoming_txt.getText() + "") != null
						&& !"".equals(baobaoxiaoming_txt.getText() + "")) {
					bean.setBaby_name(baobaoxiaoming_txt.getText().toString()
							.trim());
				} else {
					Toast.makeText(ActivityBabyAdd.this, "宝宝名为空", 1000).show();
					onResume();
					return;

				}
				Log.e("hu...", babysex_txt.getText() + "");
				if ((babysex_txt.getText() + "").equals("男")) {
					bean.setSex("1");
				} else if ((babysex_txt.getText() + "").equals("女")) {
					bean.setSex("0");
				}
				if (babymoren_txt.getText().toString().equals("是")) {

					bean.setIs_default(1 + "");
				} else if (babymoren_txt.getText().toString().equals("否")) {
					bean.setIs_default(0 + "");
				} else {
					bean.setIs_default("未知");

				}
				String birthtime = babybirth_txt.getText().toString();
				try {

					birth = BirthTime(birthtime);
					long time2 = System.currentTimeMillis() / 1000;
					if (time2 < birth) {
						ByAlert.alert("宝宝还未出生哦");
						return;
					} else {
						bean.setBirthday(birth + "");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				babyPresenter.add(bean);
				finish();
			}

			long BirthTime(String birthtime) throws ParseException {
				// TODO Auto-generated method stub
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyy年MM月dd日");
				Date date = simpleDateFormat.parse(birthtime);
				long timeStemp = date.getTime();
				timeStemp = timeStemp / 1000;
				return timeStemp;
			}
		});
	}

	private OnClickListener onclicklister = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.boy_tv:
				// 性别男
				putchangesex("男");
				mSexPopupWindow.dismiss();
				break;
			case R.id.girl_tv:
				// 性别女
				putchangesex("女");
				mSexPopupWindow.dismiss();
				break;
			case R.id.secrecy_tv:
				// 性别保密
				putchangesex("保密");
				mSexPopupWindow.dismiss();
				break;
			case R.id.yes_tv:
				// 是
				putchange("是");
				yesNoPopupWindow.dismiss();
				break;
			case R.id.no_tv:
				// 否
				putchange("否");
				yesNoPopupWindow.dismiss();
				break;
			case R.id.baby_birth_layout:

				// 当点击DatePickerDialog控件的设置按钮时，调用该方法
				DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						// 修改日历控件的年，月，日
						// 这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
						dateAndTime.set(Calendar.YEAR, year);
						dateAndTime.set(Calendar.MONTH, monthOfYear);
						dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						// //将页面TextView的显示更新为最新时间
						try {
							upDateDate();
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				};
				DatePickerDialog dateDlg = new DatePickerDialog(
						ActivityBabyAdd.this, d,
						dateAndTime.get(Calendar.YEAR),
						dateAndTime.get(Calendar.MONTH),
						dateAndTime.get(Calendar.DAY_OF_MONTH));

				dateDlg.show();
				break;
			default:
				break;
			}
		}

	};

	private void upDateDate() throws ParseException {
		babybirth_txt.setText(sdf.format(dateAndTime.getTime()));
	}

	private void putchangesex(String sex) {
		// TODO Auto-generated method stub
		babysex_txt.setText(sex);
	}

	private void putchange(String text) {
		// TODO Auto-generated method stub
		babymoren_txt.setText(text);
	}

	@Override
	public void update(Observable observable, Object data) {
	}

}
